package com.buer.train.api;

import com.buer.train.business.domain.*;
import com.buer.train.business.service.DictService;
import com.buer.train.business.service.VersionService;
import com.buer.train.common.annotation.Auth;
import com.buer.train.common.eumn.ResultEnum;
import com.buer.train.common.utils.BaseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/sys")
@Api(tags = {"系统模块接口"})
public class SystemController {

    @Autowired
    private VersionService versionService;

    @Autowired
    private DictService dictService;

    @RequestMapping(value = "/version/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("版本列表")
    @Auth(value = "sys:version:list")
    public BaseResult versionList(@RequestBody Map<String, Object> params) {
        PageInfo<VersionDO> versionList = versionService.getVersionList(params);
        Map map = new HashMap<>();
        map.put("list", versionList.getList());
        map.put("total", versionList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS, map);
    }

    @RequestMapping(value = "/version/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("版本添加")
    @Auth(value = "sys:version:add")
    public BaseResult versionAdd(@RequestBody VersionDO versionDO) {
        versionDO.setType(0);
        versionDO.setCreateDate(new Date());
        int save = versionService.save(versionDO);
        if (save>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/version/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID获取版本信息")
    @Auth
    public BaseResult versionGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        //版本信息
        Integer id = Integer.valueOf(params.get("id") + "");

        VersionDO versionDO = versionService.get(id);

        return BaseResult.build(ResultEnum.SUCCESS,versionDO);
    }

    @RequestMapping(value = "/version/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("版本编辑")
    @Auth(value = "sys:version:edit")
    @Transactional
    public BaseResult versionEdit(@RequestBody VersionDO versionDO) {
        //版本更新
        versionDO.setUpdateDate(new Date());
        int update = versionService.update(versionDO);
        if (update>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/version/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除版本")
    @Auth(value = "sys:version:remove")
    public BaseResult versionRemove(@RequestBody Map<String, Object> params) {

        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        //删除用户信息
        Integer id = Integer.valueOf(params.get("id") + "");
        int remove = versionService.remove(id);

        if (remove>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/version/getNew", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取最新版本")
    @Auth
    public BaseResult getNew(@RequestBody Map<String, Object> params) {
        VersionDO one = versionService.getOne(params);
        return BaseResult.build(ResultEnum.SUCCESS,one);
    }

    @RequestMapping(value = "/dict/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("字典列表")
    @Auth(value = "sys:dict:list")
    public BaseResult dictList(@RequestBody Map<String, Object> params){
        PageInfo<DictDO> deviceCodeList = dictService.getDictList(params);
        HashMap map = new HashMap();
        map.put("list",deviceCodeList.getList());
        map.put("total",deviceCodeList.getTotal());
        return BaseResult.build(ResultEnum.SUCCESS,map);
    }


    @RequestMapping(value = "/dict/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("新增字典数据")
    @Auth(value = "sys:dict:add")
    public BaseResult addDict(@RequestBody DictDO dictDO){
        dictDO.setCreateTime(new Date());
        dictDO.setUpdateTime(new Date());
        if (dictService.save(dictDO)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/dict/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("修改字典数据")
    @Auth(value = "sys:dict:update")
    public BaseResult updateDict(@RequestBody DictDO dictDO){
        DictDO dict = dictService.get(dictDO.getId());
        dict.setKey(dictDO.getKey());
        dict.setValue(dictDO.getValue());
        dict.setDesc(dictDO.getDesc());
        dict.setUpdateTime(new Date());

        if(dictService.update(dict)>0){
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @RequestMapping(value = "/dict/remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID删除字典数据")
    @Auth(value = "sys:dict:remove")
    public BaseResult removeDict(@RequestBody Map<String, Object> params){
        if (Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        dictService.remove(id);
        return BaseResult.build(ResultEnum.SUCCESS);
    }

    @RequestMapping(value = "/dict/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("根据ID查询字典数据")
    @Auth
    public BaseResult dictGet(@RequestBody Map<String, Object> params) {
        if(Objects.isNull(params.get("id"))){
            return BaseResult.build(ResultEnum.PARAME_ERROR);
        }
        Integer id = Integer.valueOf(params.get("id") + "");
        DictDO dictDO = dictService.get(id);
        return BaseResult.build(ResultEnum.SUCCESS,dictDO);
    }

}
